package com.rcpcompany.examples.flows.pages;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.rcpcompany.examples.flows.directors.FirstDirector;
import com.rcpcompany.ui.flows.AbstractFlowPage;
import com.rcpcompany.ui.flows.IFlowPage;

public class Page1Page extends AbstractFlowPage implements IFlowPage {

	private Text myLastName;
	private Text myFirstName;
	protected final FirstDirector myDirector;
	private Button myAddressWantedButton;

	public Page1Page(FirstDirector d) {
		super("Page 1", null);
		myDirector = d;
	}

	@Override
	public void createControl(Composite parent) {
		Composite top = new Composite(parent, SWT.NONE);
		top.setLayout(new GridLayout(2, false));
		setControl(top);

		final Label firstNameLabel = new Label(top, SWT.NONE);
		firstNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		firstNameLabel.setText("&First Name");

		myFirstName = new Text(top, SWT.BORDER);
		myFirstName.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				updated();
			}
		});
		final GridData gd_firstName = new GridData(SWT.FILL, SWT.CENTER, true, false);
		myFirstName.setLayoutData(gd_firstName);

		final Label lastNameLabel = new Label(top, SWT.NONE);
		final GridData gd_lastNameLabel = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
		lastNameLabel.setLayoutData(gd_lastNameLabel);
		lastNameLabel.setText("&Last Name");

		myLastName = new Text(top, SWT.BORDER);
		myLastName.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				updated();
			}
		});
		final GridData gd_lastName = new GridData(SWT.FILL, SWT.CENTER, true, false);
		myLastName.setLayoutData(gd_lastName);

		myAddressWantedButton = new Button(top, SWT.CHECK);
		myAddressWantedButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				updated();
			}
		});
		final GridData gd_supplyAddressButton = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
		myAddressWantedButton.setLayoutData(gd_supplyAddressButton);
		myAddressWantedButton.setText("Supply &Address");
	}

	protected void updated() {
		myDirector.setName(myFirstName.getText(), myLastName.getText(), myAddressWantedButton.getSelection());
		pageUpdated();
	}

	@Override
	public void refreshState() {
	}

	@Override
	public void saveState() {
	}

}
